package com.LeeCode;

/**
 * Z字形变换
 */

public class Code006 {
    public static void main(String[] args) {
        String s = "PAYPALISHIRING";
        System.out.println(new Code006().convert(s, 3));
    }

    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        StringBuilder res = new StringBuilder();
        StringBuilder[] auxs = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            auxs[i] = new StringBuilder();
        }

        int direction = -1, index = 0;
        for (int i = 0; i < s.length(); i++) {
            if (index == 0 || index == numRows - 1) {
                direction *= -1;
            }
            auxs[index].append(s.charAt(i));
            index += direction;

        }
        for(StringBuilder aux : auxs) {
            res.append(aux);
        }
        return res.toString();
    }
}
